package uicgas.register.GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.SpringLayout;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JButton;

import uicgas.globaldata.ControlMessage;
import uicgas.globaldata.Messages;
import uicgas.globaldata.Password;
import uicgas.globaldata.Pump;
import uicgas.globaldata.SelfAnnouncement;
import uicgas.register.RegisterLogic;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PasswordEntry extends JFrame
{

	private JPanel contentPane;
	private JPasswordField passwordField;
	private boolean flag = false;
	private String password;
	private Pump pump;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					//PasswordEntry frame = new PasswordEntry();
					//frame.setVisible(true);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @param pump 
	 */
	public PasswordEntry(final PumpSelectionScreen thePumpScreen, final RegisterLogic register, Pump pump)
	{
		this.pump = pump;
		setTitle("UIC Gas - Password Entry");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 500, 200);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);

		passwordField = new JPasswordField();
		passwordField.setHorizontalAlignment(SwingConstants.CENTER);
		passwordField.setFont(new Font("Tahoma", Font.BOLD, 18));
		sl_contentPane.putConstraint(SpringLayout.NORTH, passwordField, 75,
				SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, passwordField, 10,
				SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, passwordField, -50,
				SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, passwordField, -10,
				SpringLayout.EAST, contentPane);
		contentPane.add(passwordField);

		JLabel lblPleaseProvideThe = new JLabel(
				"Please provide the password to recommission this pump.");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblPleaseProvideThe,
				20, SpringLayout.NORTH, contentPane);
		lblPleaseProvideThe.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblPleaseProvideThe.setHorizontalAlignment(SwingConstants.CENTER);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblPleaseProvideThe, 0,
				SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, lblPleaseProvideThe,
				-120, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, lblPleaseProvideThe, 0,
				SpringLayout.EAST, contentPane);
		contentPane.add(lblPleaseProvideThe);

		JButton btnOk = new JButton("OK");
		btnOk.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				password = getPassword();
				startValidatePassword(register);
				exitThisScreen();
			}
		});
		btnOk.setFont(new Font("Tahoma", Font.PLAIN, 16));
		sl_contentPane.putConstraint(SpringLayout.NORTH, btnOk, 50,
				SpringLayout.NORTH, passwordField);
		sl_contentPane.putConstraint(SpringLayout.WEST, btnOk, 0,
				SpringLayout.WEST, passwordField);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, btnOk, -10,
				SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, btnOk, -250,
				SpringLayout.EAST, contentPane);
		contentPane.add(btnOk);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				exitThisScreen();
			}
		});
		btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 16));
		sl_contentPane.putConstraint(SpringLayout.NORTH, btnCancel, 0,
				SpringLayout.NORTH, btnOk);
		sl_contentPane.putConstraint(SpringLayout.WEST, btnCancel, 250,
				SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, btnCancel, -10,
				SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, btnCancel, 0,
				SpringLayout.EAST, passwordField);
		contentPane.add(btnCancel);
	}

	private void exitThisScreen()
	{
		//this.setVisible(false);
		this.dispose();
	}
	private void setVisibility(boolean condition)
	{
		this.setVisible(condition);
	}

	private String getPassword()
	{
		String password = "";
		while (!flag)
		{
			char[] Password = new char[passwordField.getPassword().length]; 
			Password = passwordField.getPassword();
			for(int i=0; i<Password.length; i++)
			{
				password = password + Password[i];
			}
			if(password.length()>=1)
			{
				flag = true;
			}
			//System.out.println(Password);
		}
		return password;
	}
	protected void startValidatePassword(RegisterLogic register)
	{
		Password password1 = new Password(password, register.getID());
		ControlMessage messageOut = new ControlMessage(Messages.VALIDATERECOMMPASSWORD, password1);
		register.getCommunication().sendData(messageOut);
		Pause(6);
		if(register.getPasswordValidity())
		{
			SelfAnnouncement myAnouncment = new SelfAnnouncement(register.getID(), pump.getID());
			ControlMessage myMessage = new ControlMessage(Messages.RECOMMISSIONPUMP, myAnouncment);
			register.getCommunication().sendData(myMessage);
		}
		
	}
	private void Pause(int iTimeInSeconds)
	{
		long time_start, time_end;

		time_start = System.currentTimeMillis();
		time_end = System.currentTimeMillis() + (iTimeInSeconds * 1000);

		do
		{
			time_start = System.currentTimeMillis();

		}
		while (time_start < time_end);
		{
			//do nothing, just wait
		}
	}

}
